package com.anolesoft.amljmes.fortyone.controller;

import com.anolesoft.amljmes.billing.dto.CategoryInfoDto;
import com.anolesoft.amljmes.fortyone.dto.BasRclMachinesDto;
import com.anolesoft.amljmes.fortyone.dto.MesStoveBaseDto;
import com.anolesoft.amljmes.fortyone.service.MesStoveBaseService;
import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.yr.demo.BaseQueryParamVO;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.*;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Arrays;

@RestController
@CrossOrigin
@RequestMapping(value="/mesStoveBase")
@Slf4j
public class MesStoveBaseController {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @SuperBuilder
    @ToString(callSuper = true)
    @EqualsAndHashCode(callSuper=true)
    public static class DemoVO extends BaseQueryParamVO implements Serializable {
        private String furnaceid;//炉次编号
        private String gyffjc;//工艺方法简称
        private String machinesname;//设备名称
        private String machinescode;//设备编号
        private String plant;//所在厂房
        private String fortyoneCode;//四十一厂制造记录条码
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "GMT+8")
        private LocalDate pgsj;//派工时间
        private String factoryId;//厂代号
    }

    @Autowired
    MesStoveBaseService mesStoveBaseService;

    /**
     * 林雪
     * 质量信息填写弹出页
     * @param map
     * @return
     */
    @PostMapping(value="/queryById",produces= MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryMesStoveBaseById(@RequestBody Map<String, String> map) {
        String objuid = map.get("objuid");
        MesStoveBaseDto mesStoveBaseDto=mesStoveBaseService.queryMesStoveBaseById(objuid);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesStoveBaseDto);
    }

    /**
     * 林雪
     * 根据炉次查询
     * @param furnaceid
     * @return
     */
    @PostMapping(value="/queryByLc",produces= MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult queryMesStoveBaseByLc(@RequestParam("furnaceid") String furnaceid) {
        MesStoveBaseDto mesStoveBaseDto=mesStoveBaseService.queryMesStoveBaseByLc(furnaceid);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesStoveBaseDto);
    }

    /**
     * 林雪
     * 根据工艺方法简称查询
     * @param gyffjc
     * @return
     */
    @PostMapping(value="/queryByGyffjc",produces= MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult queryMesStoveBaseByGyffjc(@RequestParam("gyffjc") String gyffjc) {
        MesStoveBaseDto mesStoveBaseDto=mesStoveBaseService.queryMesStoveBaseByGyffjc(gyffjc);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesStoveBaseDto);
    }

    /**
     * 林雪
     * 根据设备名称查询
     * @param devicename
     * @return
     */
    @PostMapping(value="/queryBySbmc",produces= MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult queryMesStoveBaseBySbmc(@RequestParam("devicename") String devicename) {
        MesStoveBaseDto mesStoveBaseDto=mesStoveBaseService.queryMesStoveBaseBySbmc(devicename);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesStoveBaseDto);
    }

    /**
     * 林雪
     * 根据所在厂房查询
     * @param plant
     * @return
     */
    @PostMapping(value="/queryBySzcf",produces= MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult queryMesStoveBaseBySzcf(@RequestParam("plant") String plant) {
        MesStoveBaseDto mesStoveBaseDto=mesStoveBaseService.queryMesStoveBaseBySzcf(plant);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesStoveBaseDto);
    }

    /**
     * 林雪
     * 根据派工时间查询
     * @param pgsj
     * @return
     */
    @PostMapping(value="/queryByPgsj",produces= MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult queryMesStoveBaseByPgsj(@RequestParam("pgsj") Date pgsj) {
        MesStoveBaseDto mesStoveBaseDto=mesStoveBaseService.queryMesStoveBaseByPgsj(pgsj);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesStoveBaseDto);
    }

    /**
     * 林雪 开工按钮
     * @param objuids
     * @return
     */
    @PostMapping(value = "/updateByKg", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult updateMesStoveBaseKg(@RequestBody String[] objuids) {
        System.out.println(">>>>>>updateKg");
        List<String> asList = null;
        if (objuids != null) {
            asList = Arrays.asList(objuids);
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,
                mesStoveBaseService.updateMesStoveBaseKg(asList));
    }

    /**
     * 林雪 完工按钮
     * @param objuids
     * @return
     */
    @PostMapping(value = "/updateByWg", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult updateMesStoveBaseWg(@RequestBody String[] objuids) {
        System.out.println(">>>>>>updateWg");
        List<String> asList = null;
        if (objuids != null) {
            asList = Arrays.asList(objuids);
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,
                mesStoveBaseService.updateMesStoveBaseWg(asList));
    }

    // 完工按钮,改变派工状态
    @PostMapping(value = "/updateByPg", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult updateMesStoveBasePg(@RequestBody String[] objuids) {
        System.out.println(">>>>>>updatePg");
        List<String> asList = null;
        if (objuids != null) {
            asList = Arrays.asList(objuids);
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,
                mesStoveBaseService.updateMesStoveBasePg(asList));
    }

    /**
     * 林雪
     * 修改状态
     * @param queryInfoDto
     * @return
     */
    @PostMapping(value="/updateByStateTx", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult updateMesStoveBaseStateTx(@RequestBody  MesStoveBaseDto queryInfoDto){
        System.out.println(">>>>>>update");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setTxr(loginUser.getEmpUid());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesStoveBaseService.updateMesStoveBaseStateTx(queryInfoDto));
    }
    /**
     * 林雪
     * 修改状态
     * @param queryInfoDto
     * @return
     */
    @PostMapping(value="/updateByStateJy", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult updateMesStoveBaseStateJy(@RequestBody  MesStoveBaseDto queryInfoDto){
        System.out.println(">>>>>>update");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setWgr(loginUser.getEmpUid());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesStoveBaseService.updateMesStoveBaseStateJy(queryInfoDto));
    }

    /**
     * 林雪
     * 带条件查询
     * @param
     * @return
     */
    @PostMapping(value="/query",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult queryMesStoveBaseInfo(DemoVO demoVO){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        demoVO.setFactoryId(loginUser.getFacCode());

        List<MesStoveBaseDto> allList = mesStoveBaseService.queryMesStoveBaseInfo(demoVO);
        System.out.println(">>>>>>"+allList.toString());
        int pn =  demoVO.getPageNo().intValue();
        int ps = demoVO.getPageSize().intValue();
        int endIndex = pn * ps > allList.size() ? allList.size() : pn*ps;
        List<MesStoveBaseDto> list = allList.subList( (pn-1) *ps, endIndex  );
        PageResult pageResult = new PageResult(list, demoVO.getPageSize(), demoVO.getPageNo(), allList.size());
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }
    /**
     * 设备动态下拉
     */
    @PostMapping(value="/all",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult MachinesnameAll(){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String factoryId=loginUser.getFacCode();
    	List<BasRclMachinesDto> list = mesStoveBaseService.MachinesnameAll(factoryId);
        AjaxResult success = AjaxResult.success();
        success.put("data", list);
        return success;
    }

    //删除
    @PostMapping(value="/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult deleteMesStoveBase(@RequestBody String[] objuids){
        System.out.println(">>>>>>delete");
        List<String> asList = null;
        if (objuids != null) {
            asList = Arrays.asList(objuids);
        }
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,mesStoveBaseService.deleteMesStoveBase(asList));
    }

    //新增
    @PostMapping(value="/add", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public AjaxResult addMesStoveBase(@RequestBody MesStoveBaseDto queryInfoDto){
        System.out.println(">>>>>>add");
        LoginUser loginUser = SecurityUtils.getLoginUser();
        queryInfoDto.setCjr(loginUser.getEmpUid());
        queryInfoDto.setFactoryId(loginUser.getFacCode());
        queryInfoDto.setPgr(loginUser.getEmpUid());
        String devicename = queryInfoDto.getDevicename();
        String deviceid = devicename.split("-")[1];
        queryInfoDto.setDeviceid(deviceid);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, mesStoveBaseService.addMesStoveBase(queryInfoDto));
    }

    /**
     * fd 根据设备名查询
     * @param machinesname
     * @return
     */
    @PostMapping(value = "/queryByMachinesname", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult queryBasRclMachines(@RequestParam("machinesname") String machinesname) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String factoryId=loginUser.getFacCode();
        BasRclMachinesDto BasRclMachinesDto = mesStoveBaseService.queryBasRclMachines(machinesname,factoryId);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, BasRclMachinesDto);
    }
}
